package sjtu.pirate.report;

import java.io.File;
import java.io.IOException;

import sjtu.pirate.analyzer.PirateAnalyzer;
import biz.source_code.miniTemplator.MiniTemplator;
import biz.source_code.miniTemplator.MiniTemplator.TemplateSyntaxException;
/**
 * @author wilbur.ma@qq.com
 * @date 2011.05.31
 * Use MiniTemplator library to generate html pages.
 */
public abstract class ReportTemplate {
	protected MiniTemplator template;
	protected String reportDir;
	protected String stdPath;
	protected PirateAnalyzer analyzer;
	
	public ReportTemplate(String templateFile, String reportDir, 
			String stdPath, PirateAnalyzer analyzer) 
			throws TemplateSyntaxException, IOException {
		this.template = new MiniTemplator(templateFile);
		this.reportDir = reportDir;
		this.stdPath = stdPath;
		this.analyzer = analyzer;
	}
	
	protected abstract void setTemplateParams();
	
	protected void setVar(String name, String value) {
		template.setVariable(name, value);
	}
	
	protected void setVar(String name, int value) {
		template.setVariable(name, value);
	}
	
	protected void addBlock(String blockName) {
		template.addBlock(blockName);
	}
	
	public void compile() {
		setTemplateParams();
	}
	
	public String generateOutput() {
		return template.generateOutput();
	}
	
	public void generateOutputFile() throws IOException {
		File f = new File(reportDir.toString() + stdPath);
		if (!f.getParentFile().exists())
			f.getParentFile().mkdirs();
		template.generateOutput(f.getAbsolutePath());
		System.out.println("New report file generated: " + f.getAbsolutePath());
	}
}
